package com.example.mybatisdebugtest.service.impl;

import com.example.mybatisdebugtest.entity.User;
import com.example.mybatisdebugtest.entity.UserExample;
import com.example.mybatisdebugtest.mapper.UserMapper;
import com.example.mybatisdebugtest.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public User login(String name, String password) {
        UserExample example = new UserExample();

        UserExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(name).andPasswordEqualTo(password);

        List<User> list = userMapper.selectByExample(example);
        if (list.size() <= 0 ) return null;
        return list.get(0);
    }

    @Override
    public boolean register(String name, String password) {

        User user = new User();
        user.setStatus(1);
        user.setName(name);
        user.setPassword(password);
        user.setCreateTime(new Date());

        return userMapper.insert(user)>0;
    }

    @Override
    public boolean updatePassword(String name, String password) {

        UserExample example = new UserExample();

        UserExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(name);

        System.out.println(example);

        List<User> list = userMapper.selectByExample(example);
        System.out.println(list);

        if (list.size() <= 0 ) return false;

        User user = list.get(0);
        user.setPassword(password);

        return userMapper.updateByPrimaryKey(user)>0;

    }

    @Override
    public boolean updateStatus(Integer id, String statusStr) {

        User user = queryUser(id);

        if (user == null) return false;

        user.setStatus(getStatus(statusStr));

        return userMapper.updateByPrimaryKey(user)>0;

    }

    @Override
    public boolean update(User user) {
        return userMapper.updateByPrimaryKey(user)>0;
    }

    @Override
    public boolean deleteByStatus(String statusStr) {
        Integer status = getStatus(statusStr);
        if (status != 2) return false;
        UserExample example = new UserExample();

        UserExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(status);

        return userMapper.deleteByExample(example)>0;
    }

    @Override
    public boolean deleteById(Integer id) {
        return userMapper.deleteByPrimaryKey(id)>0;
    }

    @Override
    public List<User> queryUserByStatus(String statusStr) {
        UserExample example = new UserExample();

        UserExample.Criteria criteria = example.createCriteria();

        Integer status = getStatus(statusStr);

        if (status < 0) return null;

        criteria.andStatusEqualTo(status);

        return userMapper.selectByExample(example);
    }
    private Integer getStatus(String statusStr){
        int status = -1;

        if (statusStr == null || "".equals(statusStr)) return -1;

        if ("禁用".contains(statusStr) || statusStr.contains("禁用")) {
            status = 0;
        } else if ("正常".contains(statusStr) || statusStr.contains("正常")) {
            status = 1;
        } else if ("离职".contains(statusStr) || statusStr.contains("离职")) {
            status = 2;
        }
        return status;
    }

    @Override
    public List<User> queryUserByNameOrStatusStr(String name,String statusStr) {
        UserExample example = new UserExample();

        UserExample.Criteria criteria = example.createCriteria();
        criteria.andNameLike(name).andStatusEqualTo(getStatus(statusStr));

        return userMapper.selectByExample(example);
    }

    @Override
    public List<User> queryUserAll() {
        return userMapper.selectByExample(null);
    }

    @Override
    public User queryUser(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }
}
